﻿using System.Linq;
using MusicStore.Entities;

namespace MusicStore.Data.Repository
{
	/// <summary>
	/// a set of filters that operate on an IQueryable interface
	/// this is the 'Pipeline' pattern (also knows and fluent interface usage)
	/// </summary>
	public static class AlbumFilters
	{
		public static Album ByAlbumId(
			this IQueryable<Album> query,
			int id)
		{
			return query.Where(a => a.Id == id).FirstOrDefault();
		}

		public static IQueryable<AlbumSummary> ByPage(
			this IQueryable<AlbumSummary> query,
			int pageNumber,
			int pageSize)
		{
			int skip = (pageNumber) * pageSize;
			return query.Skip(skip).Take(pageSize);
		}

		public static IQueryable<AlbumSummary> Search(this IQueryable<AlbumSummary> query, string text)
		{
			if (!string.IsNullOrEmpty(text))
				return query.Where(album => album.Title.Contains(text) || album.Genre.Contains(text) ||
					album.Author.Contains(text) || album.Title.Contains(text));
			return query;
		}
	}
}